﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Service;
using JinfuyuanProject.Service.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI;
using System.Xml.Linq;

namespace JinfuyuanProject.Controllers
{/*
  * 2025-4-21
  * 员工页面
  * 周梦飞
  */
    public class StaffController : Controller
    {
        private readonly IStaffService _staffService;
        private readonly IRoleService _roleService;

        public StaffController(IStaffService staffService,IRoleService roleService)
        {
            _staffService = staffService;
            _roleService= roleService;
        }

        #region 页面
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult StaffList()
        {
            return View();
        }
        public ActionResult AddStaff(int? id)
        {
            ViewBag.role=_roleService.GetList();
            if (id == null) {
                ViewBag.NewStaffNumber = _staffService.GetNumber();
            }
            else
            {
                ViewBag.staffid=id;
            }
            return View();
        }
        #endregion

        #region 接口
        //树型菜单数据接口
        [HttpPost]
        public JsonResult GetStaffTree()
        {
            List<StaffTree> result = _staffService.GetStaffTree();
            return Json(result);
        }
        public List<StaffTree> GetAreaChildNode() 
        {
            List<StaffTree> result = _staffService.GetAreaChildNode();
            return result;
        }
        //员工列表接口
        public JsonResult StaffAPI(int page, int limit,int? RoleId = 0,string name=null,string phone=null)
        {
            StaffFilter filter = new StaffFilter();
            filter.Name = name;
            filter.Phone = phone;
            filter.RoleId = RoleId;
            filter.page = page;
            filter.limit = limit;
            filter.Isdelete = 0;
            return Json(_staffService.StaffAPI(filter), JsonRequestBehavior.AllowGet);
        }
        //上传头像图片
        [HttpPost]
        public JsonResult UploadImg()  
        {
            Hashtable hs = new Hashtable();
            HttpPostedFileBase file = Request.Files[0];
            if (file == null)
                hs["code"] = 1;
            string path = Server.MapPath("/Assets/Images/") + file.FileName;
            file.SaveAs(path);
            hs["code"] = 0;
            hs["url"] = "/Assets/Images/" + file.FileName;
            //接收二进制文件流
            return Json(hs);
        }
        //添加一个员工
        [HttpPost]
        public JsonResult AddStaff(Staff staff)
        {
            Hashtable hs = new Hashtable();
            if (!ModelState.IsValid)
            {
                hs["code"] = 1;
                hs["msg"] = "实体校验不合格";
            }
            //新添加的员工默认为在职状态
            staff.State = 0; 
            staff.CreateTime = DateTime.Now;
            staff.Isdelete = 0;
            _staffService.Insert(staff);
            hs["code"] = 0;
            hs["msg"] = "success";
            return Json(hs);
        }
        //更新一个员工的数据
        [HttpPost]
        public JsonResult UpdateStaff(Staff staff)
        {
            Hashtable hs = new Hashtable();
            if (!ModelState.IsValid)
            {
                hs["code"] = 1;
                hs["msg"] = "实体校验不合格";
            }
            Staff oldStaff=_staffService.GetModel(staff.Id);
            oldStaff.Name = staff.Name;
            oldStaff.Phone = staff.Phone;
            oldStaff.CardId = staff.CardId;
            oldStaff.BankCard= staff.BankCard;
            oldStaff.RoleId = staff.RoleId;
            oldStaff.ImgUrl = staff.ImgUrl;
            oldStaff.OnboardingTime = staff.OnboardingTime;
            oldStaff.Pwd = staff.Pwd;
            oldStaff.Sex = staff.Sex;
            oldStaff.Age = staff.Age;
            _staffService.Update(oldStaff);
            hs["code"] = 0;
            hs["msg"] = "success";
            return Json(hs);
        }
        //删除一个员工(逻辑删除)
        [HttpPost]
        public JsonResult Del(int id)  
        {
            Hashtable hs = new Hashtable();
            Staff staff = _staffService.GetModel(id);
            if (staff == null)
                hs["code"] = 1;
            staff.Isdelete=1;
            _staffService.Update(staff);
            hs["code"] = 0;
            hs["msg"] = "success";
            return Json(hs);
        }
        //根据id得到一个员工实体
        [HttpPost]
        public JsonResult GetStaff(int id)
        {
            Hashtable hs = new Hashtable();
            Staff staff= _staffService.GetModel(id);
            if (staff == null)
                hs["code"] = 1;
            hs["code"] = 0;
            hs["data"] = staff;
            return Json(hs);
        }
        //改变员工的状态（在职或停职）
        [HttpPost]
        public JsonResult StaffState(int id)
        {
            Hashtable hs = new Hashtable();
            Staff staff=_staffService.GetModel(id);
            //改变员工的状态
            staff.State=1-staff.State;
            _staffService.Update(staff);
            hs["code"] = 0;
            hs["msg"] = "success";
            return Json(hs);
        }
        #endregion
    }
}